package com.moleq.mgpos.activities;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.moleq.adapter.EJrptSelectionAdpater;
import com.moleq.adapter.ShowViewSignatureAdpater;
import com.moleq.adapter.ViewSignatureSelectionAdpater;
import com.moleq.base.MoleQBaseActivity;
import com.moleq.base.MoleQBaseDialog;
import com.moleq.device.DeviceManager;
import com.moleq.dialog.DateSettingSelectionDialog;
import com.moleq.dialog.InputDateInfoDialog;
import com.moleq.dialog.MessageDialog;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.logic.PosPrinter;
import com.moleq.logic.TransactionManager;
import com.moleq.models.GlobalInfo;
import com.moleq.models.PosEJ;
import com.moleq.models.PosSetting;
import com.moleq.models.PosTender;
import com.moleq.models.PosTrns;
import com.moleq.models.TenderSignature;
import com.moleq.mgpos.activities.R;
import com.moleq.service.PosEJService;
import com.moleq.service.PosTenderService;
import com.moleq.service.PosTrnsService;
import com.moleq.service.ServiceManager;
import com.moleq.service.TenderSignatureService;
import com.moleq.util.ImageUtil;
import com.moleq.util.MoleQUtil;

public class ViewSignatureActivity extends MoleQBaseActivity {
	private ViewSignatureSelectionAdpater sigAdapter = null;
	private ShowViewSignatureAdpater  mShowViewSignatureAdpater = null;
	private List<String[]> list = new ArrayList<String[]>();
	private List<String[]> listShow = new ArrayList<String[]>();
	private PosTrnsService posTranService = null;
	private TenderSignatureService mTenderSignatureService = null;
	private PosTenderService posTenderService = null;
	
	private ListView lv_signature;
	private ListView lv_showsignature;
	private EditText et_startDate, et_endDate;
//	private TextView tv_content;
	private Button btn_go, btn_print, btn_exit;
	private MessageDialog mMessageDialog;

	private Calendar cal = Calendar.getInstance();
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	private DateSettingSelectionDialog mdssDialog;
	private InputDateInfoDialog ididDialog;
	private DeviceManager mDeviceManager;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_view_signature);

//		tv_content = (TextView) findViewById(R.id.tv_content);
		lv_signature = (ListView) findViewById(R.id.lv_signature);
		lv_showsignature = (ListView) findViewById(R.id.lv_showsignature);
		et_startDate = (EditText) findViewById(R.id.et_startDate);
		et_endDate = (EditText) findViewById(R.id.et_endDate);
		btn_go = (Button) findViewById(R.id.btn_go);
		btn_print = (Button) findViewById(R.id.btn_print);
		btn_exit = (Button) findViewById(R.id.btn_exit);

		lv_signature.setOnItemClickListener(onItemClickListener);
		et_startDate.setText(sdf.format(cal.getTime()));
		et_endDate.setText(sdf.format(cal.getTime()));
		et_startDate.setOnClickListener(onClickListener);
		et_endDate.setOnClickListener(onClickListener);
		btn_go.setOnClickListener(onClickListener);
		btn_print.setOnClickListener(onClickListener);
		btn_exit.setOnClickListener(onClickListener);

		posTranService = ServiceManager.getInstance().mPosTrnsService;
		mTenderSignatureService = ServiceManager.getInstance().mTenderSignatureService;
		posTenderService = ServiceManager.getInstance().mPosTenderService;
		
		mdssDialog = new DateSettingSelectionDialog(this);
		ididDialog = new InputDateInfoDialog(this, this);
		PosSetting item = ServiceManager.getInstance().mPosSettingService.Find();
		if(item == null){
			GlobalInfo.getInstance().calendar_type = false;
		}else{
			GlobalInfo.getInstance().calendar_type = item.isCalendarType();
		}
		if (sigAdapter == null)
			sigAdapter = new ViewSignatureSelectionAdpater(this, list);
		lv_signature.setAdapter(sigAdapter);
		
		if (mShowViewSignatureAdpater == null)
			mShowViewSignatureAdpater = new ShowViewSignatureAdpater(this, listShow);
		lv_showsignature.setAdapter(mShowViewSignatureAdpater);
		
		// setup printer
				// mDeviceManager.setPrinter(new CasioPrinter());
		mDeviceManager = DeviceManager.getInstance();
		mDeviceManager.setPrinter(new PosPrinter());
		mDeviceManager.printer.onConnect();
	}

	OnClickListener onClickListener = new OnClickListener() {

		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_go:
//				tv_content.setText("");
				String sStartDate = MoleQUtil.fixDateTime2(
						String.valueOf(et_startDate.getText()), "yyyyMMdd",
						"yyyy-MM-dd");
				String sEndDate = MoleQUtil.fixDateTime2(
						String.valueOf(et_endDate.getText()), "yyyyMMdd",
						"yyyy-MM-dd");
				int iStartDate = Integer.valueOf(sStartDate);
				int iEndDate = Integer.valueOf(sEndDate);
				if(iStartDate > iEndDate) {
					Message("The end date cannot be earlier than start date.",
							mMessageDialog.MESSAGE_BOX_OK,
							mMessageDialog.ICON_INFO);
					return;
				}
				int days = MoleQUtil.getGapCount(et_startDate.getText().toString(), et_endDate.getText().toString());
				if (days > 90) {
					Message("Between two dates cannot be longer than 90 days.",
							mMessageDialog.MESSAGE_BOX_OK,
							mMessageDialog.ICON_INFO);
					return;
				}
				
				list = posTranService.getSignatureTransByBeginDateToEndDate(sStartDate, sEndDate);
				sigAdapter.setSource(list);
				sigAdapter.notifyDataSetChanged();
				sigAdapter.ScrollToRow(-1);
				break;
			case R.id.btn_print:
//				String context = tv_content.getText().toString();
				String context = getEJContent();
				if(context.trim().equals("")) return;
				PosPrinter printer = new PosPrinter();
				if(!printer.isBusy()){
					printer.onConnect();
					printer.print(context);
					printer.cutPaper();
					printer.onDisconnect();
				}
				break;
			case R.id.btn_exit:
				Message("Do you want to exit ?",
						mMessageDialog.MESSAGE_BOX_YES_NO,
						mMessageDialog.ICON_INFO);
				break;
			case R.id.et_startDate:
				if (GlobalInfo.getInstance().calendar_type) {
					mdssDialog.show(R.id.et_startDate,et_startDate.getText().toString());
				}else{
					ididDialog.show(R.id.et_startDate,et_startDate.getText().toString());
				}
				break;
			case R.id.et_endDate:
				if (GlobalInfo.getInstance().calendar_type) {
					mdssDialog.show(R.id.et_endDate,et_endDate.getText().toString());
				}else{
					ididDialog.show(R.id.et_endDate,et_endDate.getText().toString());
				}
				break;
			}
		}
	};

	public OnItemClickListener onItemClickListener = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			sigAdapter.ScrollToRow(position);
			listShow = new ArrayList<String[]>();
//			setEJContent(list.get(position).getEj_id());
//			listShow = posTenderService.FindByTranNo(Integer.valueOf(list.get(position)[0]));
			List<TenderSignature> listSignature =  mTenderSignatureService.FindByTranNo(Integer.valueOf(list.get(position)[0]));
			
			for (int i = 0; i < listSignature.size(); i++) {
				TenderSignature mTenderSignature = listSignature.get(i);
				PosTender mPosTender = posTenderService.Find(mTenderSignature.getTs_trn_no(), mTenderSignature.getTs_tender_id());
				PosTrns mPosTrns = ServiceManager.getInstance().mPosTrnsService.Find(mTenderSignature.getTs_trn_no());
				TransactionManager.getInstance().dDoneDate = MoleQUtil.ChangeDate(mPosTrns.getPt_done_date()+mPosTrns.getPt_done_time(),"yyyyMMddhhmmss");
				String signatureContent = MoleQUtil.parsePrintContent(TransactionManager.getInstance().getViewCreditContent(mPosTender,mPosTrns));
				String[] object = {signatureContent,mTenderSignature.getTs_signature_value()};
				listShow.add(object);
			}
			
			mShowViewSignatureAdpater.setSource(listShow);
			mShowViewSignatureAdpater.notifyDataSetChanged();
			
		}
	};
	
	public String getEJContent(){
		String content = "";
//		for (int i = 0; i < listShow.size(); i++) {
//			if (i > 0) {
//				content = content + "\n\n" + listShow.get(i).getEj_content();
//			} else {
//				content = listShow.get(i).getEj_content();
//			}
//		}
		return content;
	}

	private OnClickMessageListener onClickMessageListener = new OnClickMessageListener() {

		public void onClickYes() {
			finish();
		}

		public void onClickNo() {
			mMessageDialog.dismiss();
		}

		public void onClickOK() {
			mMessageDialog.dismiss();
		}
	};

	public void Message(String text, int type, int icon) {
		mMessageDialog = new MessageDialog(ViewSignatureActivity.this);
		mMessageDialog.setStyle(type);
		mMessageDialog.setMessageIcon(icon);
		mMessageDialog.setMessage(text);
		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
		mMessageDialog.show();
	}
	
	@Override
	public void onCloseWithReturn(Class<? extends MoleQBaseDialog> pramClass, int viewId, String parm) {
		super.onCloseWithReturn(pramClass, viewId, parm);
		
		switch (viewId) {
		case R.id.et_startDate:
			et_startDate.setText(parm);
			break;
		case R.id.et_endDate:
			et_endDate.setText(parm);
			break;
		default:
			break;
		}
	}
}
